﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guia3.Models;

using Raven.Client.Document;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Linq;
//using Raven.Client.Shard;
//using Raven.Storage.Esent;

namespace Guia3.Controllers
{
    public class FormatoMidiaController : Controller
    {
        // get
        public ActionResult List()
        {
            //var db = HttpContext.Application["db"] as DocumentStore;// EmbeddableDocumentStore;
            //using (var session = db.OpenSession()) //*** abre a sessão para trabalhar com o banco de dados
            using (var session = MvcApplication.DocDB.OpenSession())
            {
                var FormatoMidias = from formatomidia in session.Query<FormatoMidia>()
                               select formatomidia;
                return View(FormatoMidias);

            }
        }

        //
        // GET: /FormatoMidia/
        public ActionResult New() //abertura inicial do formulario.
        {
            FormatoMidia FormMidia = new FormatoMidia { Id = 0, Descricao = "", Tipo = "", Tamanho = "", Preco = 0, Template = "" };
            return View("Edit", FormMidia);
        }

        public ActionResult ToEdit(int Id) //localiza e mostra registro pra edição
        {
            using (IDocumentSession session = MvcApplication.DocDB.OpenSession())
            {
                var results = (from formatomidia in session.Query<FormatoMidia>()
                               where formatomidia.Id == Id
                               select formatomidia).ToArray();
                return View("Edit", results[0]);
            }
        }

        [HttpPost] // ****  Este bloco recebe os dados do formulário e Grava....
        public ActionResult Save(FormCollection campos)
        {

            using (IDocumentSession session = MvcApplication.DocDB.OpenSession())
            {
                FormatoMidia FormMidia = session.Load<FormatoMidia>(int.Parse(campos["Id"]));
                if (FormMidia == null)
                {
                    FormMidia = new FormatoMidia
                    {
                        Id = int.Parse(campos["Id"]),
                        Descricao = campos["Descricao"],
                        Tipo = campos["Tipo"],
                        Tamanho = campos["Tamanho"],
                        Preco = int.Parse(campos["Preco"]),
                        Template = campos["Template"]
                    };
                    session.Store(FormMidia);
                }
                else
                {
                    FormMidia.Descricao = campos["Descricao"];
                    FormMidia.Tipo = campos["Tipo"];
                    FormMidia.Tamanho = campos["Tamanho"];
                    FormMidia.Preco = int.Parse(campos["Preco"]);
                    FormMidia.Template = campos["Template"];
                }
                session.SaveChanges();
            }

            return RedirectToAction("List", "FormatoMidia");
        }

        public ActionResult Delete(int Id)
        {
            using (var session = MvcApplication.DocDB.OpenSession())
            {
                FormatoMidia FormMidia = session.Load<FormatoMidia>(Id);
                session.Delete(FormMidia);

                session.SaveChanges();
            }
            return RedirectToAction("List", "FormatoMidia");
        }

    }
}


